package com.tuyan.aixiang.common;

import com.tuyan.aixiang.common.lang.VoidFunction;
import com.tuyan.aixiang.enums.LaunchMode;
import com.tuyan.aixiang.model.NoticeCountResult;
import com.tuyan.aixiang.model.UserInfo;
import com.tuyan.aixiang.util.StorageUtil;

import java.util.HashMap;
import java.util.Map;

public class AppData {

    private static final String TAG = "AppData";

    private static Integer lanuchMode;

    private static String token;

    private static UserInfo userInfo;

    private static String ysToken;


    public static void clearAll() {
        setToken(null);
        setUserInfo(null);
        clearUserDataChangeCallback();
        clearNoticeCountChangeCallback();
    }

    public static String getYsToken() {
        return ysToken;
    }

    public static void setYsToken(String t) {
        ysToken = t;
    }

    public static String getToken() {
        if (null == token) {
            String storedToken = StorageUtil.getString(StorageKey.TOKEN, null);
            token = storedToken;
        }
        return token;
    }

    public static void setToken(String t) {
        token = t;
        StorageUtil.setString(StorageKey.TOKEN, t);
    }

    public static Integer getLanuchMode() {
        if (null == lanuchMode) {
            Integer lastLaunchMode = StorageUtil.getInt(StorageKey.LAUNCH_MODE, LaunchMode.CONSUMER.getValue());
            lanuchMode = lastLaunchMode;
        }
        return lanuchMode;
    }

    public static void setLanuchMode(Integer mode) {
        StorageUtil.setInt(StorageKey.LAUNCH_MODE, mode);
        lanuchMode = mode;
    }

    public static UserInfo getUserInfo() {

        if (null == userInfo) {
            userInfo = StorageUtil.getObject(StorageKey.USER_INFO, UserInfo.class);
            if (null == userInfo) userInfo = new UserInfo();
        }
        return userInfo;
    }

    public static void setUserInfo(UserInfo user) {
        userInfo = user;
        StorageUtil.setObject(StorageKey.USER_INFO, user);
        if (null != user && null != user.getToken()) {
            setToken(user.getToken());
            setLanuchMode(user.getRole());
        }
        if (userDataChangeCallbacks.size() > 0) {
            for (VoidFunction func : userDataChangeCallbacks.values()) {
                func.call();
            }
        }
    }

    public static Map<String, VoidFunction> userDataChangeCallbacks = new HashMap<>();

    public static Map<String, VoidFunction> noticeCountChangeCallbacks = new HashMap<>();

    public static void addUserDataChangeCallback(String flag, VoidFunction func) {
        userDataChangeCallbacks.put(flag, func);
    }


    public static void clearUserDataChangeCallback() {
        userDataChangeCallbacks.clear();
    }

    public static void addNoticeCountChangeCallback(String flag, VoidFunction func) {
        noticeCountChangeCallbacks.put(flag, func);
    }

    public static void clearNoticeCountChangeCallback() {
        noticeCountChangeCallbacks.clear();
    }

    public static NoticeCountResult globalNoticeCount = new NoticeCountResult();

    public static void setNoticeCount(NoticeCountResult noticeCount) {
        if (null != noticeCount) {
            globalNoticeCount = noticeCount;
            if (noticeCountChangeCallbacks.size() > 0) {
                for (VoidFunction func : noticeCountChangeCallbacks.values()) {
                    func.call();
                }
            }
        }
    }

}
